﻿@typeparam TItem where TItem : VrrpInstance

<MDataTable Items="@Items"
            Loading="_isLoading"
            HideDefaultFooter
            Dense>
    <HeaderContent>
        <Tr>
            <Th>实例名称</Th>
            <Th>状态</Th>
            <Th>虚拟路由器ID</Th>
            <Th>优先级</Th>
            <Th>虚拟IP</Th>
            <Th>操作</Th>
        </Tr>
    </HeaderContent>
    
    <ItemContent Context="item">
        <MTd>@item.Item.Name</MTd>
        <MTd>
            <MChip Color="@(item.Item.State == "MASTER" ? "success" : "info")"
                   Small>
                @item.Item.State
            </MChip>
        </MTd>
        <MTd>@item.Item.VirtualRouterId</MTd>
        <MTd>@item.Item.Priority</MTd>
        <MTd>
            @if (item.Item.VirtualIpAddresses.Any())
            {
                <MChip Small Class="mr-1">
                    @item.Item.VirtualIpAddresses.First()
                </MChip>

                @if (item.Item.VirtualIpAddresses.Count > 1)
                {
                    <MTooltip Top>
                        <ActivatorContent>
                            <MChip Small>+@(item.Item.VirtualIpAddresses.Count - 1)</MChip>
                        </ActivatorContent>
                        <ChildContent>
                            @string.Join(", ", item.Item.VirtualIpAddresses.Skip(1))
                        </ChildContent>
                    </MTooltip>
                }
            }
        </MTd>
        <MTd>
            <MButton Icon
                     Color="primary"
                     OnClick="@(() => EditItem(item.Item))">
                <MIcon>mdi-pencil</MIcon>
            </MButton>
            <MButton Icon
                     Color="error"
                     OnClick="@(() => RemoveItem(item.Item))">
                <MIcon>mdi-delete</MIcon>
            </MButton>
        </MTd>
    </ItemContent>
    
    <FooterContent>
        <MCol Cols="12">
            <MButton Color="primary"
                     Outlined
                     Block
                     OnClick="OnAdd">
                <MIcon Left>mdi-plus</MIcon>
                添加VRRP实例
            </MButton>
        </MCol>
    </FooterContent>
</MDataTable>

@if (_editDialog)
{
    <VrrpInstanceEditorDialog @bind-Value="_editDialog"
                              @bind-Item="_currentItem"
                              TItem="TItem"
                              OnSave="HandleSave" />
}

@code {
    [Parameter]
    public List<TItem> Items { get; set; }

    [Parameter]
    public EventCallback<List<TItem>> ItemsChanged { get; set; }

    [Parameter]
    public EventCallback OnAdd { get; set; }

    [Parameter]
    public EventCallback<TItem> OnRemove { get; set; }
    [Inject]
    public IPopupService PopupService { get; set; }

    private bool _isLoading = false;
    private bool _editDialog = false;
    private TItem _currentItem;

    private async Task EditItem(TItem item)
    {
        _currentItem = item;
        _editDialog = true;
    }

    private async Task RemoveItem(TItem item)
    {
        var confirm = await PopupService.ConfirmAsync(
            "确认删除",
            $"确定要删除VRRP实例 {item.Name} 吗?",
            "删除",
            AlertTypes.Warning);
        
        if (confirm)
        {
            await OnRemove.InvokeAsync(item);
        }
    }

    private async Task HandleSave(TItem item)
    {
        _editDialog = false;
        await ItemsChanged.InvokeAsync(Items);
    }
}